﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
    public class NewsController : Fcom.VNInsurance.Web.CultureHelper.CultureController
    {
        //
        // GET: /News/
        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles="13,14")]
        public ActionResult AddItem()
        {
            ViewBag.NcatIDList = new SelectList(db.NewsCategories, "ID", "Name");
            return View();
        }
        [Authorize(Roles = "13,14")]
        [HttpPost, ValidateInput(false)]
        public ActionResult AddItem(Fcom.VNInsurance.Web.Models.NewsModel model)
        {
            if (ModelState.IsValid)
            {
                Fcom.VNInsurance.Web.Models.New news = new Models.New();
                news.NCatID = model.CatID;
                news.Title = model.Title;
                db.News.InsertOnSubmit(news);
                db.SubmitChanges();
                Fcom.VNInsurance.Web.Models.NewsLang newslang = new Models.NewsLang();
                //newslang.
                newslang.LangID = Request.Cookies["_culture"]["lang"];
                newslang.FullContent = HttpUtility.HtmlEncode(model.Content);
                newslang.ShortContent =  HttpUtility.HtmlEncode(model.ShortContent);
                newslang.Title = model.Title;
                newslang.PostbyID = 1;
                newslang.NewsID = news.ID;
                newslang.PostDate = DateTime.Now;
                db.NewsLangs.InsertOnSubmit(newslang);
                db.SubmitChanges();
                return RedirectToAction("ManageCat", new {CatID=newslang.New.NCatID});
            }
            ViewBag.NcatIDList = new SelectList(db.NewsCategories, "ID", "Name");
            return View();
        
        }
        [Authorize(Roles = "13,14")]
        public ActionResult Manage()
        {
          
              
                return View();
            
        }
        [Authorize(Roles = "13,14")]
        public ActionResult ManageCat(int CatID)
        {
         
              
                return View("ManageCat", db.NewsCategories.Where(n => n.ID == CatID).FirstOrDefault());
   
          
        }
        [Authorize(Roles = "13,14")]
        [HttpPost, ValidateInput(false)]
        public ActionResult EditNews(Fcom.VNInsurance.Web.Models.NewsModel model)
        {
            if (ModelState.IsValid)
            {
                var newslang = db.NewsLangs.Where(m => m.NewsID == model.ID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
                if (newslang == null)
                {
                    newslang = new Models.NewsLang();
                    //newslang.
                    newslang.LangID = Request.Cookies["_culture"]["lang"];
                    newslang.FullContent = HttpUtility.HtmlEncode(model.Content);
                    newslang.ShortContent = HttpUtility.HtmlEncode(model.ShortContent);
                    newslang.Title = model.Title;
                    newslang.PostbyID = 1;
                    newslang.NewsID = model.ID;
                    newslang.PostDate = DateTime.Now;
                    db.NewsLangs.InsertOnSubmit(newslang);
                    db.SubmitChanges();
                }
                else
                {
                    newslang.LangID = Request.Cookies["_culture"]["lang"];
                    newslang.FullContent = HttpUtility.HtmlEncode(model.Content);
                    newslang.ShortContent = HttpUtility.HtmlEncode(model.ShortContent);
                    newslang.Title = model.Title;
                    newslang.PostbyID = 1;
                    newslang.NewsID = model.ID;
                    newslang.PostDate = DateTime.Now;
                    db.SubmitChanges();
                }
                return RedirectToAction("ManageCat", new { CatID=newslang.New.NCatID});
            }
            ViewBag.NcatIDList = new SelectList(db.NewsCategories, "ID", "Name");
            return View();           
        }
         [Authorize(Roles = "13,14")]   
        public ActionResult EditNews(int NewsID)
        {
            var newslang = db.NewsLangs.Where(m => m.NewsID == NewsID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            if (newslang != null)
            {
                Fcom.VNInsurance.Web.Models.NewsModel newsmodel = new Models.NewsModel();
                newsmodel.ID = newslang.NewsID;
                newsmodel.Title = newslang.Title;
                newsmodel.ShortContent = HttpUtility.HtmlDecode(newslang.ShortContent);
                newsmodel.Content = HttpUtility.HtmlDecode(newslang.FullContent);
                newsmodel.CatName = newslang.New.NewsCategory.Name;
               //  ViewBag.NcatIDList = from ncat in db.NewsCategories select new SelectListItem { Text = ncat.Name, Value = ncat.ID.ToString(), Selected = (ncat.ID == newslang.New.NCatID) };
                //IEnumerable<SelectListItem> catitems = from cat in listcategory select new SelectListItem { Text = cat.Name, Value = cat.AutoCID.ToString(), Selected = listcategory[0].AutoCID== cat.AutoCID };

                return View(newsmodel);
            }
            else
            {
                var news = db.News.Where(n=>n.ID==NewsID).FirstOrDefault();
                if (news != null)
                {
                    Fcom.VNInsurance.Web.Models.NewsModel newsmodel = new Models.NewsModel();
                    newsmodel.ID = news.ID;
                    newsmodel.Title = news.Title;
                    ViewBag.NcatIDList = from ncat in db.NewsCategories select new SelectListItem { Text = ncat.Name, Value = ncat.ID.ToString(), Selected = (ncat.ID == news.NCatID) };
                    newsmodel.CatName = news.NewsCategory.Name;
                    return View(newsmodel);
                }
                else 
                {
                    return RedirectToAction("AddItem");
                }
        
            }
        }
        //newsdetail/
         public ActionResult NewsDetail(int NewsID) {

             var newslang =( from n in db.NewsLangs where n.LangID == Request.Cookies["_culture"]["lang"] && n.NewsID == NewsID select n).FirstOrDefault();
            
             return View(newslang);

         }
        [Authorize(Roles = "13,14")]
        public ActionResult GetJsonNews()
        {
            List<Fcom.VNInsurance.Web.ViewModel.NewsViewModel> list = new List<ViewModel.NewsViewModel>();
            Fcom.VNInsurance.Web.ViewModel.NewsViewModel newsmodel;
            var news = db.News.OrderByDescending(m=>m.ID);
            foreach (var ne in news)
            {
                newsmodel = new ViewModel.NewsViewModel();
                newsmodel.LangID = Request.Cookies["_culture"]["lang"];
                newsmodel.NewID = ne.ID;
                newsmodel.CatName = ne.NewsCategory.Name;
                var newsstranle = ne.NewsLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
                if (newsstranle != null)
                {
                    newsmodel.Title = newsstranle.Title;
                   // newsmodel.ShortContent = newsstranle.ShortContent;
                    //newsmodel.FullContent = newsstranle.FullContent;
                    newsmodel.PostDate = newsstranle.PostDate.ToString("dd/MM/yyyy");
                    newsmodel.Translated = true;

                }
                else
                {
                    newsmodel.Translated = false;
                    newsmodel.Title = ne.Title;
                   
                }
                list.Add(newsmodel);
            }
            return Json(list,JsonRequestBehavior.AllowGet);             
        }
        [Authorize(Roles = "13,14")]
        public ActionResult DeleteNewsByID(int NewsID)
        {
            var langnews = db.NewsLangs.Where(n => n.NewsID == NewsID);
            db.NewsLangs.DeleteAllOnSubmit(langnews);
            var news = db.News.Where(n => n.ID == NewsID);
            db.News.DeleteAllOnSubmit(news);
            db.SubmitChanges();
            return Json(new { success=true});
        }
        [Authorize(Roles = "13,14")]
        public ActionResult GetJsonCatNews(int CatID)
        {
            List<Fcom.VNInsurance.Web.ViewModel.NewsViewModel> list = new List<ViewModel.NewsViewModel>();
            Fcom.VNInsurance.Web.ViewModel.NewsViewModel newsmodel;
            var news = db.News.OrderByDescending(m => m.ID).Where(m=>m.NCatID==CatID);
            foreach (var ne in news)
            {
                newsmodel = new ViewModel.NewsViewModel();
                newsmodel.LangID = Request.Cookies["_culture"]["lang"];
                newsmodel.NewID = ne.ID;
                newsmodel.CatName = ne.NewsCategory.Name;
                var newsstranle = ne.NewsLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
                if (newsstranle != null)
                {
                    newsmodel.Title = newsstranle.Title;
                    // newsmodel.ShortContent = newsstranle.ShortContent;
                    //newsmodel.FullContent = newsstranle.FullContent;
                    newsmodel.PostDate = newsstranle.PostDate.ToString("dd/MM/yyyy");
                    newsmodel.Translated = true;

                }
                else
                {
                    newsmodel.Translated = false;
                    newsmodel.Title = ne.Title;

                }
                list.Add(newsmodel);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}

